package com.etl.plugin.datasource.pool.abs;

import com.etl.base.DataSourceInfo;
import com.etl.plugin.datasource.pool.IBasePool;
import org.apache.poi.ss.formula.functions.T;

import java.sql.Connection;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 连接池
 */

public abstract class BaseAbsPool<T> implements IBasePool<T> {

    //空闲队列
    private LinkedBlockingDeque<T> freeQueue;

    //活动队列
    private LinkedBlockingDeque<T> activeQueue;

    //当前线程数量
    private AtomicInteger sum;

    //最大等待时间长  AtomicInteger保证线程安全
    private long maxWait;

    //基础配置
    private DataSourceInfo dataSourceInfo;

    @Override
    public void init(DataSourceInfo dataSourceInfo) {


    }

    /**
     * 获取
     * @return
     */
    @Override
    public T pull() {
        //取空闲线程
        T coon = freeQueue.poll();
        //加入活动队列
        try {
            activeQueue.put(coon);
            return coon;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 归还
     * @param conn
     */
    @Override
    public void back(T conn) {
        try {
            //不用放进去 不是清空
            activeQueue.remove(conn);
            //中间夹杂判断
            freeQueue.put(conn);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 销毁
     */
    @Override
    public void destroy() {
        while(true){
            T conn = freeQueue.poll();
            closeConn(conn);
        }
    }

    /**
     * 创建
     * @param dataSourceInfo
     * @return
     */
    @Override
    public T newConn(DataSourceInfo dataSourceInfo) {

        return null;
    }

    /**
     * 刷新
     */

    @Override
    public void refresh() {

    }

    /**
     * 心跳
     */
    @Override
    public void heartbeat() {

    }

}
